package org.jeecg.modules.demo.project.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.project.entity.PjProject;
import org.jeecg.modules.demo.project.service.IPjProjectService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.pubuyorder.entity.PuBuyOrder;
import org.jeecg.modules.demo.soSaleOrder.entity.SoSaleOrder;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: pj_project
 * @Author: jeecg-boot
 * @Date:   2021-12-15
 * @Version: V1.0
 */
@Api(tags="pj_project")
@RestController
@RequestMapping("/project/pjProject")
@Slf4j
public class PjProjectController extends JeecgController<PjProject, IPjProjectService> {
	@Autowired
	private IPjProjectService pjProjectService;
	
	/**
	 * 分页列表查询
	 *
	 * @param pjProject
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "pj_project-分页列表查询")
	@ApiOperation(value="pj_project-分页列表查询", notes="pj_project-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PjProject pjProject,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try{
			QueryWrapper<PjProject> queryWrapper = QueryGenerator.initQueryWrapper(pjProject, req.getParameterMap());
			Page<PjProject> page = new Page<PjProject>(pageNo, pageSize);
			IPage<PjProject> pageList = pjProjectService.page(page, queryWrapper);
			return Result.OK(pageList);
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *   添加
	 *
	 * @param pjProject
	 * @return
	 */
	@AutoLog(value = "pj_project-添加")
	@ApiOperation(value="pj_project-添加", notes="pj_project-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PjProject pjProject) {
		try{
			pjProject.setDelFlag(CommonConstant.DEL_FLAG_0);
			pjProjectService.save(pjProject);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	 @GetMapping("/getPjProject")
	 public Result getPjProject() {
		 List<PjProject> logicDeletedProjectList = pjProjectService.getPjProject();
		 return Result.ok(logicDeletedProjectList);
	 }
	 @RequestMapping(value = "/revertLogicDeletedPjProject", method = RequestMethod.PUT)
	 public Result revertLogicDeletedPjProject(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		 String Ids = jsonObject.getString("projectIds");
		 if (StringUtils.isNotBlank(Ids)) {
			 pjProjectService.revertLogicDeletedPjProject(Arrays.asList(Ids.split(",")));
			 return Result.ok("还原成功");
		 }else{
			 return Result.error(500,"系统错误！");
		 }
	}


	/**
	 *  编辑
	 *
	 * @param pjProject
	 * @return
	 */
	@AutoLog(value = "pj_project-编辑")
	@ApiOperation(value="pj_project-编辑", notes="pj_project-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PjProject pjProject) {
		try{
			pjProjectService.updateById(pjProject);
			return Result.OK("编辑成功!");

		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "pj_project-通过id删除")
	@ApiOperation(value="pj_project-通过id删除", notes="pj_project-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			List<PuBuyOrder> puBuyOrders = pjProjectService.getPuOrderByPjId(id);
			List<SoSaleOrder> soSaleOrders = pjProjectService.getSoSaleOrderByPjId(id);
			if (!puBuyOrders.isEmpty() || !soSaleOrders.isEmpty()) {
				return Result.error(300, "项目正在使用，无法删除！");
			} else {
				pjProjectService.removeById(id);
				return Result.OK("删除成功!");
			}
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(300,"系统错误！");
		}

	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "pj_project-批量删除")
	@ApiOperation(value="pj_project-批量删除", notes="pj_project-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.pjProjectService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "pj_project-通过id查询")
	@ApiOperation(value="pj_project-通过id查询", notes="pj_project-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PjProject pjProject = pjProjectService.getById(id);
		if(pjProject==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(pjProject);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param pjProject
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PjProject pjProject) {
        return super.exportXls(request, pjProject, PjProject.class, "pj_project");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PjProject.class);
    }

}
